<template>
  <div class="post" ref="post">
    <div class="cover"></div>
    <div class="wrap">
      <div class="tit">
           <div class="account"><div class="text">标题</div><input type="text" v-model="title"></div>
             <div class="password"><div class="text">简介</div><input type="text" v-model="introduce"></div>
          </div>
    </div>
     
    <div class="footer">
      <div class="head">
                    <div class="logo"><span>编辑 </span><span>文章</span></div>
                    <p>好好学习，天天敲代码~</p>
                </div>
        <div class="edifyer">
          <div class="tool">
            <a href="https://img.tg/yoren" class="el-icon-picture" target="_blank"></a>
          </div>
          <div class="markdown-editor">
          <div class="editor">
            <textarea v-model="markdownContent" @input="updatePreview"></textarea>
          </div>
        
          <div class="preview" v-html="htmlContent"></div>
        </div>
        </div>
       <div class="button">
          <div class="loginButton" @click="submit">发布文章</div>
          <div class="back" @click="back">返回</div>
      </div>
    </div>
    </div>
</template>

<script>
import MarkdownIt from 'markdown-it'
import { postArticles } from '@/api/article';
import hljs from 'highlight.js'; // 引入 highlight.js
import markdownItHighlightJs from 'markdown-it-highlightjs'; // 引入 markdown-it-highlightjs
// import { postArticles } from '@/api/article'
import alert from '@mdit/plugin-alert'
import {mark} from '@mdit/plugin-mark'
import {attrs} from '@mdit/plugin-attrs'
// import { getImageOne } from '@/api/images';


export default {
  
  data() {
    return {
      title:'',
      introduce:'',
      markdownContent: '',
      htmlContent: '',
      // imgUrl:''
    };
  },
  methods: {
    back(){
      this.$router.go(-1)

    },
    async postArticles(){
      const data={
        uid:this.$store.state.user.userInf.uid,
        // time:this.transDate(),
        time:new Date(),
        title:this.title,
        introduce:this.introduce,
        text:this.htmlContent,
        mdText:this.markdownContent
      }
      return await postArticles(data)
      // const resData=await postArticles(data)
      // if(resData.code==='000'){
      //    this.$message({
      //     message: '提交成功啦~',
      //     type: 'success',
      //     duration: 1000
      //   });
      // }
    },
    updatePreview() {
      // 使用 markdown-it 将 Markdown 文本转换为 HTML
      this.htmlContent = this.md.render(this.markdownContent);
    },
    async submit(){

       this.$confirm('确定要发表文章吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data=await this.postArticles()
        if (data.code === '000') {
          this.$message({
            message: '发表成功哦~',
            type: 'success',
            duration: 1000
          });
          //优化体验
          this.$store.commit('article/setIndex',true)
          this.$router.push('/mine')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });


      // this.postArticles()
    },
    //  transDate() {
    //   const date = new Date()
    //   const year = date.getFullYear()
    //   const month = ('0' + (date.getMonth() + 1)).slice(-2)
    //   const day = ('0' + (date.getDate())).slice(-2)
    //   return `${year}-${month}-${day}`
    // }
  },
   async created() {
    // 初始化 markdown-it 实例
    this.md = new MarkdownIt({
      html: true, // 允许渲染 HTML 标签
      linkify: true, // 自动链接某些文本为链接
      table: true,
      highlight: (str, lang) => {
        if (lang && hljs.getLanguage(lang)) {
          try {
            return hljs.highlight(lang, str).value;
          } catch (err) { console.log(err)}
        }

        try {
          return hljs.highlightAuto(str).value;
        } catch (err) { console.log(err) }

        return ''; // 使用空字符串表示未识别的语言
      }
    });
    this.md.use(markdownItHighlightJs);
    this.md.use(alert);
    this.md.use(mark);
    this.md.use(attrs);
    
    
    
    // 初始化时更新预览
    this.updatePreview();


   
  },
  async mounted(){
    //  const data = await getImageOne()
    //  this.imgUrl=data.data.src
    //  this.$refs.post.style.backgroundImage=`url(${this.imgUrl})`
  }
};
</script>
<style src="@/assets/css/sspai.less" lang="less" scoped></style>
<style lang="less" scoped>
.cover{
  position: fixed;
  top: 0;
  background-color:  #220F4Bd9;
  width: 100vw;
  height: 100vh;
  z-index: 1;
}

.head {
    width: 100%;
    text-align: center;
    
    padding-left: 30px;
    margin-bottom: 10px;
   
    position: relative;
    z-index: 4;
}

.head p {
    color:#fff;
    font-size: 12px;
    line-height: 30px;
}

.logo  :nth-child(1) {
    color: #E3C420;
    font-weight: 900;
    font-size: 25px;
}

.logo  :nth-child(2) {
    color: #FF1A89;
    font-weight: 900;
    font-size: 25px;
}
.footer{
  height: 100%;
  // padding: 0 20px;
}
.post{
  overflow-y: hidden;
  height: 100vh;
  background:url('@/assets/regBk.avif') no-repeat;
  background-size: 100%;
  //  background-color:  #220F4B; 
  padding: px;
  
}

.wrap{
  padding: 20px;
}
.tit{
 
  z-index: 4;
  position: relative;
  padding: 20px;
  /* border: 4px solid #2F00FF; */
  border-radius: 20px;
  background-color: rgba(7, 3, 30, 0.4);
 
}

.text{
    height: 100%;
    line-height: 40px;
    font-size: 16px;
    font-weight: bold;
    // color: #E3C420;
    color: #EC38ED;
    padding-right: 10px;
    
}
.account{
  width: 50%;
}
.password{
  width: 90%;
  margin-top: 20px;
}
.account,
.password{
    height: 40px;
    display: flex;
    /* border-bottom: 2px solid #2F00FF; */
    
    
}
input{
    flex: 1;
    outline: none;
    border: none;
    text-indent: 20px;
     color: #E3C420;
    background-color:transparent;
    border: 2px solid #2F00FF;
    border-radius: 10px;
    font-size: 18px;
    
    
}
.content{
  position: relative;
  z-index: 4;
  line-height: 40px;
    font-size: 17px;
    font-weight: bold;
    color: #FF1A89;
    padding-left: 20px;
}
.edifyer{
  z-index: 4;
  position: relative;
  padding: 0 30px;
}
.tool{
  
  height: 40px;
  background-color:#1d1629;
  line-height: 40px;
  padding: 0 20px;
  border: 4px solid  black;
  border-bottom: none;

}
.tool a{
   color: #8b7f8e;
  font-size: 20px;
}
.markdown-editor {
  
  display: flex;
  // padding-left: 20px;
  /* padding: 0 20px; */
}

.editor,
.preview {
  /* padding: 1rem; */
  box-sizing: border-box;
  height: 500px;
  color: #fff;
}
.editor{
  width: 60%;
  
}
.editor textarea {
  width: 100%;
  height: 100%;
  resize: none;
  background-color: #241c35;
  // background-color:#291f3b;
   color: #8b7f8e;
  /* background-color: #251825; */
  padding: 16px;
  outline: none;
  font-size: 18px;
  border: 4px solid  black;
}

.preview {
  flex: 1;
   color: #8b7f8e;
 background-color: #241c35;
   padding: 16px;
  outline: none;
  border: 4px solid  black;
  overflow: auto;
}


.button{
  z-index: 4;
  position: relative;
    width: 100%;
    display: flex;
    justify-content: left ;
    padding-left: 30px;
    margin-top: 30px;

}
.loginButton,
.back{
    width: 10%;
    height: 50px;
    color: black;
    text-align: center;
    line-height: 40px;
    font-size: 18px;
    font-weight: 600;
    transition: 0.5s;
    cursor: pointer;
}
.loginButton{
    background-color: #fe54c3;
    border: 5px solid  #6934aa;
}
.back{
    background-color: #9a56ee;
     border: 5px solid  #6934aa;
    margin-left: 20px;
}
.loginButton:hover,
.back:hover{
    background-color: #56EE8F;
    color: #f3f0f0;
}



</style>